import { ref } from 'vue'
import { followDoctor } from '@/services/consult'
import type { FollowType } from '@/types/consult'
// 封装关注和取消关注复用的逻辑  usexxx
const useFollow = (type: FollowType = 'doc') => {
  const loading = ref(false)

  const follow = async (doc: { id: string; likeFlag: 0 | 1 }) => {
    loading.value = true
    try {
      await followDoctor(doc.id, type) // 关注  |  取消关注
      doc.likeFlag = doc.likeFlag === 1 ? 0 : 1
    } finally {
      // finally 不管try里面代码是否执行成功都会执行 finally里面的代码
      setTimeout(() => {
        loading.value = false
      }, 1000)
    }
  }
  return { loading, follow }
}

export { useFollow }
